<template>
  <div class="app-container">
    <div class="search">
      <!-- 搜索表单 -->
      <el-form :model="serchForm" ref="formRef">
        <el-row :gutter="20">
          <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="用户名" prop="account">
              <el-input
                v-model="serchForm.model.nickname"
                placeholder="请输入用户名"
                style="width: 200px"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="邮箱" prop="account">
              <el-input
                v-model="serchForm.model.email"
                placeholder="邮箱"
                style="width: 200px"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="8">
            <el-form-item label="授权状态">
              <el-select v-model="serchForm.model.status" placeholder="请选择">
                <el-option label="禁用" :value="0"></el-option>
                <el-option label="正常" :value="1"></el-option>
                <!-- <el-option label="待审核" :value="2"></el-option> -->
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="创建时间" prop="createAt">
              <el-date-picker
                v-model="serchForm.createAt"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间" /></el-form-item
          ></el-col> -->
        </el-row>
      </el-form>
      <!--重置 搜搜按钮 -->
      <div class="btns">
        <el-button icon="Refresh" @click="handleResetClick">重置</el-button>
        <el-button type="primary" icon="Search" @click="handleQueryClick"
          >查询</el-button
        >
        <!-- <el-button type="primary" @click="dialogFormVisible = true"
          ><el-icon><Upload /></el-icon>新增</el-button
        > -->
      </div>
    </div>

    <!-- 表单 -->
    <div class="content">
      <div class="content">
        <div class="table">
          <el-table :data="dataList" border style="width: 100%">
            <el-table-column align="center" prop="username" label="用户名" />
            <el-table-column align="center" prop="email" label="邮箱" />
            <el-table-column align="center" label="状态操作" width="100">
              <template #default="scope">
                <el-switch
                  v-model="scope.row.status"
                  :active-value="1"
                  :inactive-value="0"
                  @click="changeShow(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column
              width="180"
              align="center"
              prop="requirement"
              label="状态"
            >
              <template #default="scope">
                <el-tag v-if="scope.row.status == 0" size="small">
                  禁用
                </el-tag>
                <el-tag
                  type="success"
                  v-if="scope.row.status == 1"
                  size="small"
                >
                  正常
                </el-tag>
                <el-tag v-if="scope.row.status == 2" type="danger" size="small">
                  待审核
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="100">
              <template #default="scope">
                <el-row>
                  <el-button
                    size="small"
                    type="primary"
                    text
                    @click="resetPassword(scope.row)"
                    >重置密码</el-button
                  >
                </el-row>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="serchForm.current"
            v-model:page-size="serchForm.size"
            :page-sizes="[10, 20, 30]"
            layout="->,total,sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!-- 对话框 -->
    <el-dialog v-model="dialogFormVisible" title="新建">
      <el-form :model="form" ref="formEl">
        <el-form-item label="应用名称" label-width="110" prop="appName">
          <el-input
            v-model="form.appName"
            autocomplete="off"
            placeholder="请输入应用名称"
          />
        </el-form-item>
        <el-form-item label="购买账号" label-width="110" prop="purchaseAccount">
          <el-input
            v-model="form.purchaseAccount"
            autocomplete="off"
            placeholder="请输入购买账号"
          />
        </el-form-item>
        <el-form-item label="购买账号名称" label-width="110" prop="accountName">
          <el-input
            v-model="form.accountName"
            autocomplete="off"
            placeholder="请输入购买账号名称"
          />
        </el-form-item>
        <el-form-item label="成交时间" label-width="110" prop="transactionTime">
          <el-date-picker
            v-model="form.transactionTime"
            type="datetime"
            placeholder="成交时间"
            format="YYYY/MM/DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="使用期限" label-width="110" prop="usagePeriod">
          <el-input v-model="form.usagePeriod" autocomplete="off" />
        </el-form-item>
        <el-form-item label="到期时间" label-width="110" prop="expirationTime">
          <el-date-picker
            v-model="form.expirationTime"
            type="datetime"
            placeholder="到期时间"
            format="YYYY/MM/DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="授权状态" label-width="110" prop="status">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { formatDateTime } from "@/utils/index.js";
import {
  queryListRequest,
  disableEnableRequest,
  updatePasswordRequest,
} from "@/api/frontUser/normalUser.js";

import { ElMessage, ElMessageBox } from "element-plus";

const total = ref(0);

const serchForm = reactive({
  current: 1,
  size: 10,
  model: {},
});

onMounted(() => {
  getList();
});

let dialogFormVisible = ref(false);

// 列表数据
const dataList = ref([]);
async function getList() {
  const res = await queryListRequest(serchForm);
  console.log(res);
  dataList.value = res.data.records;
  total.value = res.data.total;
}

//重置
function handleResetClick() {
  console.log("reset");
  serchForm.current = 1;
  serchForm.size = 10;
  serchForm.model = {};
  getList();
}
//查询操作
function handleQueryClick() {
  serchForm.current = 1;
  console.log("handleQueryClick", serchForm);
  getList();
}

//分页器size事件
const handleSizeChange = (size) => {
  serchForm.size = size;
  getList();
};
//分页器current事件
const handleCurrentChange = (current) => {
  serchForm.current = current;
  getList();
};

//状态改变事件
async function changeShow(row) {
  const { status, id } = row;

  let data = {
    id,
    status,
  };
  const res = await disableEnableRequest(data);
  console.log(res);
  if (res.code === 200) {
    ElMessage.success("状态修改成功！");
    getList();
  }
}

//重置密码
const resetPassword = (row) => {
  const { id } = row;

  ElMessageBox.confirm(
    `确定重置账户为:${row.nickname}密码吗？重置后的密码为zhongjian+手机号后四位`,
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  ).then(async () => {
    const res = await updatePasswordRequest(id);
    console.log(res);
    if (res.data) {
      ElMessage.success("密码重置成功！");
      getList();
    }
  });
};
</script>
<style scoped>
label {
  font-weight: 0 !important;
}
.search {
  background-color: #fff;
  padding: 20px;
}
.search .btns {
  text-align: right;
  padding: 0 50px 10px 0;
}
.pagination {
  margin-top: 14px;
}
</style>
